package com.agileboot.attendance.domain.project.model;

import com.agileboot.attendance.domain.project.entity.AttProjectEntity;
import com.agileboot.common.exception.ApiException;
import com.agileboot.common.exception.error.ErrorCode.Business;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import java.util.Date;

/**
 * 项目模型
 *
 * @author agileboot
 */
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
public class ProjectModel extends AttProjectEntity {

    /**
     * 校验项目基本信息
     */
    public void validateProjectInfo() {
        if (getProjectName() == null || getProjectName().isEmpty()) {
            throw new ApiException(Business.PROJECT_NAME_EMPTY);
        }

        if (getProjectCode() == null || getProjectCode().isEmpty()) {
            throw new ApiException(Business.PROJECT_CODE_EMPTY);
        }

        if (getStartDate() != null && getEndDate() != null && getEndDate().before(getStartDate())) {
            throw new ApiException(Business.PROJECT_DATE_INVALID);
        }
    }

    /**
     * 检查项目是否可编辑
     * 已完成或已关闭的项目不允许编辑
     *
     * @return 是否可编辑
     */
    public boolean isEditable() {
        return !"2".equals(getStatus()) && !"3".equals(getStatus());
    }

    /**
     * 转换为实体对象
     *
     * @return 实体对象
     */
    public AttProjectEntity toEntity() {
        AttProjectEntity entity = new AttProjectEntity();
        BeanUtils.copyProperties(this, entity);
        return entity;
    }

    /**
     * 从实体创建模型
     *
     * @param entity 实体
     * @return 模型
     */
    public static ProjectModel fromEntity(AttProjectEntity entity) {
        if (entity == null) {
            return null;
        }

        ProjectModel model = new ProjectModel();
        BeanUtils.copyProperties(entity, model);
        return model;
    }
} 